package com.lhf.dajiuye.controller.dict;

import com.lhf.dajiuye.bean.dict.Dict;
import com.lhf.dajiuye.common.utils.R;
import com.lhf.dajiuye.constants.RedisCacheName;
import com.lhf.dajiuye.service.dict.DictService;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import lombok.extern.slf4j.Slf4j;
import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 获取字典
 * </p>
 *
 * @author xiayuhao
 * @since 2023-10-16
 */
@RestController
@RequestMapping("/sys/dict")
@Slf4j
@CacheConfig(cacheNames = RedisCacheName.PREFIX +"/sys/dict")
public class DictController {
    @Resource
    DictService dictService;

    /**
     * 根据type获取字典Dict list
     * @param type
     * @return
     */
    @GetMapping("/getDictListByType")
    @Cacheable
    public R getDictListByType(@RequestParam("type") String type){
        List<Dict> list = dictService.getDictListByType(type);
        //return R.ok().setData(list);
        return R.ok().put("list",list);
    }

    /**
     * 根据parentId获取字典Dict list
     * @param parentId
     * @return
     */
    @GetMapping("/getDictListByPid")
    @Cacheable
    public R getDictListByPid(@RequestParam("parentId") String parentId){
        List<Dict> list = dictService.getDictListByPid(parentId);
        //return R.ok().setData(list);
        return R.ok().put("list",list);
    }

    /**
     * 根据type获取字典Dict listTree
     * @param type
     * @return
     */
    @GetMapping("/getDictListTreeByType")
    @Cacheable
    public R getDictListTreeByType(@RequestParam("type") String type){
        List<Dict> list = dictService.getDictListTreeByType(type);
        //return R.ok().setData(list);
        return R.ok().put("list",list);
    }

    /**
     * 根据id获取字典Dict
     * @param id
     * @return
     */
    @GetMapping("/getDictById")
    @Cacheable
    public R getDictById(@RequestParam("id") String id){
        Dict dict = dictService.getDictById(id);
        return R.ok().setData(dict);
    }
}
